using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Framework.Interop
{
	///<summary>
	/// Contains strings returned from the <see cref="IShellFolder">IShellFolder</see> interface methods.
	///<seealso>
	/// Native <see cref="http://msdn.microsoft.com/en-us/library/bb759820.aspx">structure</see>
	///</seealso>
	///</summary>
	[ComVisible(false)]
	[StructLayout(LayoutKind.Sequential)]
	public struct StrRet
	{
		///<summary>
		///</summary>
		internal const int MaxCStrLen = 260;

		///<summary>
		/// Create structure with default parameters
		///</summary>
		///<param name="s">Value</param>
		///<returns>Structure that was created</returns>
		///<exception cref="ArgumentNullException"></exception>
		///<exception cref="ArgumentOutOfRangeException"></exception>
		internal static StrRet CreateDefault(string s)
		{
			if (s == null)
			{
				throw new ArgumentNullException("s");
			}

			if (Encoding.Unicode.GetByteCount(s) > MaxCStrLen - 1)
			{
				throw new ArgumentOutOfRangeException("s", s, "too long");
			}

			var sr = new StrRet {uType = StrRetType.WStr, cStr = new byte[MaxCStrLen]};
			Encoding.Unicode.GetBytes(s, 0, s.Length, sr.cStr, 0);
			return sr;
		}

		private StrRetType uType;

		[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxCStrLen)] private byte[] cStr;

		///<summary>
		/// Value
		///</summary>
		internal string Value
		{
			get { return Encoding.Unicode.GetString(cStr); }
		}
	} ;

	///<summary>
	/// Specification of the desired format of the string
	///</summary>
	internal enum StrRetType
	{
		///<summary>
		/// The string encoding is Unicode
		///<seealso>
		/// Native <see cref="http://msdn.microsoft.com/en-us/library/bb759820.aspx">constant</see>
		///</seealso>
		///</summary>
		WStr = 0x0,

		///<summary>
		/// Value indicates the number of bytes from the beginning of the item identifier list where the string is located.
		///<seealso>
		/// Native <see cref="http://msdn.microsoft.com/en-us/library/bb759820.aspx">constant</see>
		///</seealso>
		///</summary>
		Offset = 0x1,

		///<summary>
		/// The string encoding is ANSI
		///<seealso>
		/// Native <see cref="http://msdn.microsoft.com/en-us/library/bb759820.aspx">constant</see>
		///</seealso>
		///</summary>
		CStr = 0x2
	} ;
}
